<template>
  <!-- 非自定义指令写法 -->
  <!-- <div>
    <div>
      <a
        href="javascript:;"
        v-for="(item, index) of tabData"
        :class="['tab-item', { active: index === currentIndex }]"
        :key="item.id"
        @click="tabChange(index)"
      >{{ item.title }}</a>
    </div>
    <div>
      <p>{{ tabContent }}</p>
    </div>
  </div> -->

  <!-- 自定义指令写法 -->
  <div>
    <div
      v-nav-change="{
        tabClass: 'tab-item',
        activeClass: 'active',
        currentIndex
      }"
    >
      <a
        href="javascript:;"
        v-for="(item, index) of tabData"
        class="tab-item"
        :key="item.id"
        @click="tabChange(index)"
        >{{ item.title }}</a
      >
    </div>
    <div>
      <p>{{ tabContent }}</p>
    </div>
  </div>
</template>

<script>
import navChange from '../directives/navChange';

export default {
  name: 'MyTab',
  directives: {
    navChange
  },
  props: {
    tabData: {
      type: Array,
      default() {
        return [];
      }
    },
    initialIndex: {
      type: [Number, String],
      default: 0
    }
  },
  data() {
    return {
      //初始化当前组件时的默认值
      currentIndex: this.initialIndex
    };
  },
  computed: {
    //渲染tab内容
    tabContent() {
      return this.tabData[this.currentIndex].content;
    }
  },
  methods: {
    tabChange(index) {
      this.currentIndex = index;
    }
  }
};
</script>

<style lang="scss">
a {
  &.active {
    text-decoration: none;
    color: #000;
  }
}
</style>
